﻿using System;
using System.Collections.Generic;
using System.Text;
using EthernetTools.Utils;

namespace EthernetTools.DataBindinds
{
    class DependString2ByteBinding: ConrolBinding
    {
        List<ConrolBinding> dependedBindings = new List<ConrolBinding>();

        /// <summary>
        /// Get's raw data of all depended bindings
        /// </summary>
        public byte[] DependedRawData
        {
            get 
            {
                if (this.IsCorrect)
                {
                    byte[] retVal = new byte[0];

                    foreach (ConrolBinding cb in dependedBindings)
                    {
                        ByteUtils.AppendBytes(ref retVal, cb.RawData);
                    }

                    return retVal;
                }

                return null;
            }
        }

        /// <summary>
        /// Same as DependedRawData
        /// </summary>
        public override byte[] RawData
        {
            get
            {
                return DependedRawData;
            }
        }
       
        /// <summary>
        /// Is all depended bindings correct
        /// </summary>
        public override bool IsCorrect
        {
            get
            {
                foreach (ConrolBinding cb in dependedBindings)
                {
                    if (!cb.IsCorrect)
                        return false;
                }
                return true;
            }
            protected set
            {

            }
        }

        /// <summary>
        /// Binds to list of depended bindings
        /// </summary>
        /// <param name="dependBindingList"></param>
        public void Bind(List<ConrolBinding> dependBindingList)
        {
            this.dependedBindings = dependBindingList;
            base.Bind();
        }

        /// <summary>
        /// Add binding to depended bindings
        /// </summary>
        /// <param name="dependBinding"></param>
        public void BindAdd(ConrolBinding dependBinding)
        {
            if (!this.dependedBindings.Contains(dependBinding))
                dependedBindings.Add(dependBinding);

            base.Bind();
        }

        /// <summary>
        /// Remove binding from depended bindings
        /// </summary>
        /// <param name="dependBinding"></param>
        public void BindDel(ConrolBinding dependBinding)
        {
            dependedBindings.Remove(dependBinding);

            if (dependedBindings.Count == 0)
                base.UnBind();
        }
    }
}
